{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "4abc02be-4ca2-4923-b2ec-70a6b9fb86d0",
   "metadata": {},
   "outputs": [],
   "source": [
    "class MyIterator:\n",
    "    def __init__(self, value):\n",
    "        self.value = value\n",
    "\n",
    "    def __iter__(self):\n",
    "        return self\n",
    "\n",
    "    def __next__(self):\n",
    "        if self.value > 0:\n",
    "            self.value -= 1\n",
    "        else:\n",
    "            raise StopIteration\n",
    "        return self.value"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f19d3c89-e082-4670-99d9-97bf853990bd",
   "metadata": {},
   "source": [
    "jupyter 只打印最后一行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "28f9cb1f-181e-4cfe-bfe1-1ddb61c4d858",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class '__main__.MyIterator'>\n"
     ]
    }
   ],
   "source": [
    "iterator = MyIterator(6)\n",
    "print(type(iterator))\n",
    "#[x for x in iterator]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "d7b2f65c-6fa8-4eee-9d8b-4cde42303a63",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5 4 3 2 1 0\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print(*iterator)\n",
    "print(*iterator)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3f84b84a-fa73-49b1-9d38-433949ab0aa4",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
